//


#import "MB_AddReadViewController.h"
#import "MB_AddReadCollectionViewCell.h"


@interface MB_AddReadViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,MB_AddReadCollectionDelegate>
/** */
@property (nonatomic, strong) UICollectionView *collectionView;
/** */
@property (nonatomic, strong) MB_SqlView *sql;
/** 书摘*/
@property (nonatomic, copy) NSString *highlightsS;
/** 批注*/
@property (nonatomic, copy) NSString *notationalS;
/** 收获*/
@property (nonatomic, copy) NSString *summaryS;

@end

@implementation MB_AddReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _sql = [[MB_SqlView alloc]initWithData:@"ReadData" dataName:@"MoodData" success:^{} fail:^(NSError * _Nullable error) {}];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = RGB(250 ,250 ,211);
    [self setnavBatalweeTitle:@"Add Read"];
    [self.view addSubview:self.collectionView];
    [self setBtn];
}
- (void)setBtn {
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, CGRectGetMaxY(_collectionView.frame)+50, kMainScreenWidth-100, 55)];
    [button setTitle:@"Submit" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:FontPFRegular(kRHeight(20))];
    button.layer.cornerRadius = 10;
    [button setBackgroundColor:RGB(250, 128, 114)];
    [button addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MB_AddReadCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MB_AddReadCollectionViewCell class]) forIndexPath:indexPath];
    cell.indexx = indexPath.row;
    cell.delegate = self;
    cell.model = _model;
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return CGSizeMake( (kMainScreenWidth - 10), (kMainScreenWidth - 15)/2 );
    }else {
        return CGSizeMake( (kMainScreenWidth - 15)/2, (kMainScreenWidth - 15)/2 );
    }
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, kMainScreenWidth, kRHeight(20))];
    label.textColor = rgba(90, 90, 90, 1);
    label.font = FontPFRegular(kRHeight(15));
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"Did you read a book today? Take note!";
    [view addSubview:label];
    
    return view;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kMainScreenWidth, 50);
}
//MARK:cell代理
- (void)senderReadCell:(MB_AddReadCollectionViewCell *)cell text:(nonnull NSString *)text{
    switch (cell.indexx) {
        case 0:
            _highlightsS = text;
            break;
        case 1:
            _notationalS = text;
            break;
        case 2:
            _summaryS = text;
            break;
        default:
            break;
    }
    
}
//MARK:save
- (void)Action {
    NSLog(@"jkddsff");
    //
    NSDateFormatter *nowFormate = [[NSDateFormatter alloc] init];
    nowFormate.dateFormat = @"yyyy-MM-dd";
    NSString *nowTime = [nowFormate stringFromDate:[NSDate date]];
    //
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:_highlightsS = _highlightsS.length == 0 ? @"":_highlightsS forKey:@"highlights"];
    [dic setObject:_notationalS = _notationalS.length == 0 ? @"":_notationalS forKey:@"notational"];
    [dic setObject:_summaryS = _summaryS.length == 0 ? @"":_summaryS forKey:@"summary"];
    //
    if (_model == nil) {
        //
        [dic setObject:nowTime forKey:@"date"];
        [dic setObject:[self randomnum] forKey:@"iD"];
        [_sql mb_insertNewEntity:dic success:^{
            [self.navigationController popViewControllerAnimated:YES];
        } fail:^(NSError * _Nullable error) {}];
    }
    else {
        //
        [_sql mb_updateEntity:dic key:[NSString stringWithFormat:@"iD=%@",_model.iD] success:^{
            [self.navigationController popViewControllerAnimated:YES];
        } fail:^(NSError * _Nullable error) {}];
    }
}

//MARK:懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        //
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight - UI_NAV_BAR_HEIGHT-UI_TAB_BAR_HEIGHT-90) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate   = self;
        _collectionView.backgroundColor = RGB(250 ,250 ,211);
        [_collectionView registerClass:[MB_AddReadCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MB_AddReadCollectionViewCell class])];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return _collectionView;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
//
- (NSString*)randomnum {
    const int count = 6;
    NSString * source =@"125896734";
    NSMutableString *str=[[NSMutableString alloc]init];
    for (int i = 0; i < count; i++) {
        unsigned index=rand()%[source length];
        NSString * s=[source substringWithRange:NSMakeRange(index, 1)];
        [str appendString:s];
    }
    NSLog(@"-%@",str);
    return str;
}

@end
